<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.validate.min.js"></script>

<script>
var userid = <?php echo UserUtils::getUserId(); ?> ;

$(document).ready(function(){

function loadcbb(){
var dataId = $('#id').val();

        $.ajax({
                url: 'Dichvu/getJson',
                type: 'POST',
                dataType: 'JSON',
                data: {id: dataId, iduser: userid},
                success: function(result){
                $('#price').html(result.data.gia);
                       
                }
        });
}
loadcbb();

    $('#id').change( function(){
        var dataId = $(this).val();
        
        $.ajax({
                url: 'Dichvu/getJson',
                type: 'POST',
                dataType: 'JSON',
                data: {id: dataId, iduser: userid},
                success: function(result){
                $('#price').html(result.data.gia);
                        
                }
        });
	});
	
	
	$('#submit').attr('disabled',true);
	$('#alert:visible').hide();
	$('#alert-credit:visible').hide();
	function textchange() {
	var regex = /\D/;		
        var input = $("#imei").val();
        var array= input.split('\n');
        var text;
        var isError=0;
        var numRight=0;
        for(var i=0;i<array.length;i++)
        {     
        	if(array[i].length==0){}else   	
        	if(array[i].length != 15 || parseInt(array[i]).toString().length <0 || array[i].match(regex)!=null || isNaN(parseInt(array[i])) )
        	{
        		//alert("loi");
        		isError=1;	
        	}
        	else if(array[i].length==15){
        		//alert("dung");
        		numRight++;
        	}
        }
        
        // numRight la so Imei dung, tong tien = numRight * gia Credit, roi hien thi ra man hinh
        //alert(numRight + " x so Credit / Emei ");
        $('#total-imei').html(numRight);
        var price = $('#price').html();
        var total =numRight * price;
        var credit=$('#current-credit').html();
        var checkcredit=credit-total;
        
        // neu trong list co loi, disable button add de user sua loi roi moi add
        if(isError==1)
        {
        	$('#alert').show();
        }else
        {
        	$('#alert:visible').hide();
        }
        if(checkcredit<0)
        {        	
        	$('#alert-credit').show();
        }else
        {
        	$('#alert-credit:visible').hide();
        }
        if(isError==0 && checkcredit>=0)
        {
        	$('#submit').attr('disabled',false);
        }else
        {
        	$('#submit').attr('disabled',true);
        }
        $('#total-credit').html(total);

    }       
    

    // EDIT BELOW -- this will update while typing
    $("#imei").keyup(textchange);

    $("#dichvuform").submit(function() {
    	$("#imei").val($.trim($('#imei').val()));
    });
   
});
</script>
<?php
/* @var $this SiteController */

$this->pageTitle=Yii::app()->name. ' - Order';

$this->breadcrumbs=array(
	'Dịch vụ'=>array('nhomdichvu/dichvu'),
	'Order',
);

?>
<div  style="width:40%;float:left;">
<div class="right-box-item">
<h1>Thông tin user</h1>
<div style="margin:20px; font-size:12pt;">
<div id="alert" style="color:red;font-weight:bold;">Kiểm tra lại list Imei</div>
<div id="alert-credit" style="color:red;font-weight:bold;">không đủ Credit để thực hiện <br/><?php echo CHtml::link("NẠP CREDIT",array('/credit/napthecao'));?></div>
<b style="width:120px; display:inline-block;">Current credit : </b><div id="current-credit" style="display:inline;"><?php echo $cre->sodu; ?></div> Credit<br/>
<b style="width:120px; display:inline-block;">Total Imei : </b><div id="total-imei"  style="display:inline;">0</div> Imei<br/>
<b style="width:120px; display:inline-block;">Total Credit : </b><div id="total-credit"  style="display:inline;">0</div> Credit<br/>
</div>
</div>
</div>
<div class="" style="width:57%;float:right">
<div class="right-box-item">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'dichvuform',
	'enableAjaxValidation'=>false,
)); ?>
<h1>Dịch vụ Unlock</h1>
<div style="margin:20px">
<div class='row'>
<span> <b>Order</b> <b>:</b> 
<div class="row">
	<?php 
	
	//echo $form->hiddenField($models,'id',array()); ?>

<?php 

echo CHtml::dropDownList('id',null, $nhomdichvu); ?>
	</div> 
</span>
</div>
<div class='row'>
<span> <b>Giá:</b> <p id="price" style="display:inline;"><?php echo $gia; ?></p> Credit
</span>
</div>

	<div class="row">
		<textarea name='imei' id='imei' style="width:355px"></textarea>
	</div> 
<br/>
	<div class="row buttons">
		<?php echo CHtml::submitButton( 'Order',array('id'=>'submit')); ?>
	</div>
<?php $this->endWidget(); ?>
</div>
</div>
</div>

